#include <iostream>
#include <vector>

using namespace std;

template<class T>
class Node
{
public:
    T data;
    Node* next;
public:
    Node(void) : next(nullptr){}
    Node(T val) : data(val), next(nullptr){}
    ~Node()
    {
        delete next;
        next = nullptr;
    }
};


template<class T>
class UnDirGphAdjtab
{
public:
    vector<Node<T>*> adjTable;
    int numVertices;

public:
    UnDirGphAdjtab() : numVertices(0){}
    UnDirGphAdjtab(vector<T> vertices) : numVertices(vertices.size())
    {
        for(auto& i : vertices)
        {
            adjTable.emplace_back(new Node<T>(i));
        }
        
    }
    ~UnDirGphAdjtab(){}

    void addEdge(T v, T w)
    {
        int flag = 0;
        Node<T>* vnode = nullptr;
        Node<T>* wnode = nullptr;
        for(int i=0; i<adjTable.size(); i++)
        {

            if(adjTable[i]->data == v)
            {
                vnode = adjTable[i];
                while(vnode->next!=nullptr)
                {
                    vnode = vnode->next;
                }
                flag++;
            }
            if(adjTable[i]->data == w)
            {
                wnode = adjTable[i];
                while(wnode->next!=nullptr)
                {
                    wnode = wnode->next;
                }
                flag++;
            }
        }
        if(flag == 2 && vnode!=nullptr && wnode!=nullptr)
        {
            vnode->next = new Node<T>(w);
            wnode->next = new Node<T>(v);
        }
        else
        {
            std::cout<<"node isn't exist"<<std::endl;
        }
        
    }

    void printGraph(void)
    {
        Node<T>* cur = nullptr;
        for(int i=0; i<adjTable.size(); i++)
        {
            cur = adjTable[i];
            cout << cur->data << " -> ";
            cur = cur->next;
            while(cur!=nullptr)
            {
                cout << cur->data << "  ";
                cur = cur->next;
            }
            cout<<endl;
        }
    }



};

int main()
{

    vector<int> nums = {1,2,3,4,5};
    UnDirGphAdjtab<int> g(nums);
    // DirGraphMat<int> g(nums);
    
    // 添加边
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(1, 4);
    g.addEdge(2, 3);
    g.addEdge(3, 4);
    
    
    // g.addEdge('a', 'b');
    // g.addEdge('a', 'c');
    // g.addEdge('a', 'd');
    // g.addEdge('b', 'c');
    // g.addEdge('c', 'd');

    // 打印图
    g.printGraph();

    return 0;
}